<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="评标任务" prop="taskId">
        <el-select v-model="queryParams.taskId" placeholder="请选择评标任务" clearable size="small">
          <el-option v-for="item in taskList" :key="item.id" :label="item.taskName" :value="item.id" />
        </el-select>
      </el-form-item>

       <el-form-item label="工程类型" prop="typeId">
        <el-select v-model="queryParams.typeId" placeholder="请选择工程分类" clearable size="small">
          <el-option v-for="sort in sortList" :key="sort.id" :label="sort.name" :value="sort.id" />
        </el-select>
      </el-form-item>

        <el-form-item label="供应商" prop="sortId">
        <el-select v-model="queryParams.companyId" placeholder="请选择供应商" clearable size="small">
          <el-option v-for="item in companyList" :key="item.id" :label="item.companyName" :value="item.id" />
        </el-select>
      </el-form-item>

     <el-form-item label="项目名称" prop="projectName">
        <el-input
          v-model="queryParams.projectName"
          placeholder="请输入项目名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
    
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['tender:project:add']"
        >导入</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['tender:project:remove']"
        >生成底价</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['tender:project:export']"
        >导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="projectList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" type="index" align="center" show-overflow-tooltip width="50px"/>
      <el-table-column label="项目名称" align="center" prop="taskName" />
      <el-table-column label="工程类型" align="center" prop="typeName" />
      <el-table-column label="工程编号" align="center" prop="projectNo" width="150"/>
      <el-table-column label="项目名称" align="center" prop="projectName" width="200"/>
      <el-table-column label="项目特征" align="center" prop="projectFeature" width="400"/>
      <el-table-column label="计量单位" align="center" prop="projectMeasure" />
      <el-table-column label="工程量" align="center" prop="projectTotal" />
      <el-table-column label="标底价" align="center" prop="reseverPrice" />
      <el-table-column label="创建时间" align="center" prop="creatorDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.creatorDate) }}</span>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改工程项目对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="项目ID" prop="taskId">
          <el-input v-model="form.taskId" placeholder="请输入项目ID" />
        </el-form-item>
        <el-form-item label="工程分类ID" prop="typeId">
          <el-input v-model="form.typeId" placeholder="请输入工程分类ID" />
        </el-form-item>
        <el-form-item label="工程编号ID" prop="projectNo">
          <el-input v-model="form.projectNo" placeholder="请输入工程编号ID" />
        </el-form-item>
        <el-form-item label="项目名称" prop="projectName">
          <el-input v-model="form.projectName" placeholder="请输入项目名称" />
        </el-form-item>
        <el-form-item label="项目特征" prop="projectFeature">
          <el-input v-model="form.projectFeature" placeholder="请输入项目特征" />
        </el-form-item>
        <el-form-item label="计量单位" prop="projectMeasure">
          <el-input v-model="form.projectMeasure" placeholder="请输入计量单位" />
        </el-form-item>
        <el-form-item label="工程量" prop="projectTotal">
          <el-input v-model="form.projectTotal" placeholder="请输入工程量" />
        </el-form-item>
        <el-form-item label="综合单价" prop="price">
          <el-input v-model="form.price" placeholder="请输入综合单价" />
        </el-form-item>
        <el-form-item label="合价" prop="totalPrice">
          <el-input v-model="form.totalPrice" placeholder="请输入合价" />
        </el-form-item>
        <el-form-item label="底价" prop="reseverPrice">
          <el-input v-model="form.reseverPrice" placeholder="请输入底价" />
        </el-form-item>
        <el-form-item label="平均价" prop="avePrice">
          <el-input v-model="form.avePrice" placeholder="请输入平均价" />
        </el-form-item>
        <el-form-item label="创建人" prop="creatorId">
          <el-input v-model="form.creatorId" placeholder="请输入创建人" />
        </el-form-item>
        <el-form-item label="创建时间" prop="creatorDate">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.creatorDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择创建时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import { listProject, getProject, delProject, addProject, updateProject, exportProject,listTaskInfo,listSortType ,getCompanyList} from "@/api/tender/project";

export default {
  name: "reservePrice",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 工程项目表格数据
      projectList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        taskId: undefined,
        typeId: undefined,
        projectNo: undefined,
        projectName: undefined,
        projectFeature: undefined,
        projectMeasure: undefined,
        projectTotal: undefined,
        price: undefined,
        totalPrice: undefined,
        reseverPrice: undefined,
        avePrice: undefined,
        creatorId: undefined,
        creatorDate: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
        this.getSortList();
    this.getCompanyList();
    this.getTaskList();
  },
  methods: {
    /** 查询工程项目列表 */
    getList() {
      this.loading = true;
      this.queryParams.type = 1;
      listProject(this.queryParams).then(response => {
        if(response.code==='ok') {
            this.projectList = response.data.data.list;
            this.total = response.data.data.total;
            this.loading = false;
        }else{
            this.projectList = [];
            this.total = 0;
            this.loading = false;
        }
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        taskId: undefined,
        typeId: undefined,
        projectNo: undefined,
        projectName: undefined,
        projectFeature: undefined,
        projectMeasure: undefined,
        projectTotal: undefined,
        price: undefined,
        totalPrice: undefined,
        reseverPrice: undefined,
        avePrice: undefined,
        creatorId: undefined,
        creatorDate: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加工程项目";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getProject(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改工程项目";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateProject(this.form).then(response => {
                if(response.code==='ok') {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.data.msg);
              }
            });
          } else {
            addProject(this.form).then(response => {
                if(response.code==='ok') {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.data.msg);
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除工程项目编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delProject(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有工程项目数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportProject(queryParams);
        }).then(response => {
          var blob=response.data
          var url = window.URL.createObjectURL(blob);
          var a = document.createElement('a');
          a.href = url;
          a.download =(new Date()).valueOf()+'.xlsx';
          a.click();
          window.URL.revokeObjectURL(url);
        }).catch(function() {});
    },
      //工程分类
     getSortList() {
      const params = { type: 1 };
      listSortType(params).then(response => {
        this.sortList = response.data;
      });
    },
     // 供应商列表信息
       getCompanyList() {
       getCompanyList().then(response => {
        this.companyList = response.data;
      });
    },
    //任务列表
     getTaskList() {
       listTaskInfo().then(response => {
        this.taskList = response.data;
      });
     }
  }
};
</script>